# uncompyle6 version 3.2.3
# Python bytecode 3.6 (3379)
# Decompiled from: Python 3.6.8 |Anaconda custom (64-bit)| (default, Feb 21 2019, 18:30:04) [MSC v.1916 64 bit (AMD64)]
# Embedded file name: site-packages\sqlalchemy\orm\loading.py
"""private module containing functions used to convert database
rows into object instances and associated state.

the functions here are called primarily by Query, Mapper,
as well as some of the attribute loading strategies.

"""
from __future__ import absolute_import
from .. import util
from . import attributes, exc as orm_exc
from ..sql import util as sql_util
from . import strategy_options
from . import path_registry
from .. import sql
from .util import _none_set, state_str
from .base import _SET_DEFERRED_EXPIRED, _DEFER_FOR_STATE
from .. import exc as sa_exc
import collections

_new_runid = util.counter()


def instances(query, cursor, context):
    """Return an ORM result as an iterator."""
    context.runid = _new_runid()
    context.post_load_paths = {}
    filtered = query._has_mapper_entities
    single_entity = (
        len(query._entities) == 1 and query._entities[0].supports_single_entity
    )
    if filtered:
        if single_entity:
            filter_fn = id
        else:

            def filter_fn(row):
                return tuple(
                    (
                        (id(item) if ent.use_id_for_hash else item)
                        for ent, item in zip(query._entities, row)
                    )
                )

    try:
        process, labels = list(
            zip(
                *[
                    query_entity.row_processor(query, context, cursor)
                    for query_entity in query._entities
                ]
            )
        )
        if not single_entity:
            keyed_tuple = util.lightweight_named_tuple("result", labels)
        while 1:
            context.partials = {}
            if query._yield_per:
                fetch = cursor.fetchmany(query._yield_per)
                if not fetch:
                    break
            else:
                fetch = cursor.fetchall()
            if single_entity:
                proc = process[0]
                rows = [proc(row) for row in fetch]
            else:
                rows = [keyed_tuple([proc(row) for proc in process]) for row in fetch]
            for path, post_load in context.post_load_paths.items():
                post_load.invoke(context, path)

            if filtered:
                rows = util.unique_list(rows, filter_fn)
            for row in rows:
                yield row

            if not query._yield_per:
                break

    except Exception as err:
        cursor.close()
        util.raise_from_cause(err)


@util.dependencies("sqlalchemy.orm.query")
def merge_result(querylib, query, iterator, load=True):
    """Merge a result into this :class:`.Query` object's Session."""
    session = query.session
    if load:
        session._autoflush()
    autoflush = session.autoflush
    try:
        session.autoflush = False
        single_entity = len(query._entities) == 1
        if single_entity:
            if isinstance(query._entities[0], querylib._MapperEntity):
                result = [
                    session._merge(
                        attributes.instance_state(instance),
                        attributes.instance_dict(instance),
                        load=load,
                        _recursive={},
                        _resolve_conflict_map={},
                    )
                    for instance in iterator
                ]
            else:
                result = list(iterator)
        else:
            mapped_entities = [
                i
                for i, e in enumerate(query._entities)
                if isinstance(e, querylib._MapperEntity)
            ]
            result = []
            keys = [ent._label_name for ent in query._entities]
            keyed_tuple = util.lightweight_named_tuple("result", keys)
            for row in iterator:
                newrow = list(row)
                for i in mapped_entities:
                    if newrow[i] is not None:
                        newrow[i] = session._merge(
                            attributes.instance_state(newrow[i]),
                            attributes.instance_dict(newrow[i]),
                            load=load,
                            _recursive={},
                            _resolve_conflict_map={},
                        )

                result.append(keyed_tuple(newrow))

        return iter(result)
    finally:
        session.autoflush = autoflush


def get_from_identity(session, key, passive):
    """Look up the given key in the given session's identity map,
    check the object for expired state if found.
    
    """
    instance = session.identity_map.get(key)
    state = instance is not None and attributes.instance_state(instance)
    if state.expired:
        if not passive & attributes.SQL_OK:
            return attributes.PASSIVE_NO_RESULT
        if not passive & attributes.RELATED_OBJECT_OK:
            return instance
            try:
                state._load_expired(state, passive)
            except orm_exc.ObjectDeletedError:
                session._remove_newly_deleted([state])
                return

            return instance
        return


def load_on_ident(
    query, key, refresh_state=None, with_for_update=None, only_load_props=None
):
    """Load the given identity key from the database."""
    if key is not None:
        ident = key[1]
    else:
        ident = None
    if refresh_state is None:
        q = query._clone()
        q._get_condition()
    else:
        q = query._clone()
    if ident is not None:
        mapper = query._mapper_zero()
        _get_clause, _get_params = mapper._get_clause
        if None in ident:
            nones = set(
                [
                    _get_params[col].key
                    for col, value in zip(mapper.primary_key, ident)
                    if value is None
                ]
            )
            _get_clause = sql_util.adapt_criterion_to_null(_get_clause, nones)
        _get_clause = q._adapt_clause(_get_clause, True, False)
        q._criterion = _get_clause
        params = dict(
            [
                (_get_params[primary_key].key, id_val)
                for id_val, primary_key in zip(ident, mapper.primary_key)
            ]
        )
        q._params = params
    if with_for_update is not None:
        version_check = True
        q._for_update_arg = with_for_update
    else:
        if query._for_update_arg is not None:
            version_check = True
            q._for_update_arg = query._for_update_arg
        else:
            version_check = False
        q._get_options(
            populate_existing=bool(refresh_state),
            version_check=version_check,
            only_load_props=only_load_props,
            refresh_state=refresh_state,
        )
        q._order_by = None
        try:
            return q.one()
        except orm_exc.NoResultFound:
            return


def _setup_entity_query(
    context,
    mapper,
    query_entity,
    path,
    adapter,
    column_collection,
    with_polymorphic=None,
    only_load_props=None,
    polymorphic_discriminator=None,
    **kw
):
    if with_polymorphic:
        poly_properties = mapper._iterate_polymorphic_properties(with_polymorphic)
    else:
        poly_properties = mapper._polymorphic_properties
    quick_populators = {}
    path.set(context.attributes, "memoized_setups", quick_populators)
    for value in poly_properties:
        if only_load_props:
            if value.key not in only_load_props:
                continue
            value.setup(
                only_load_props=only_load_props,
                column_collection=column_collection,
                memoized_populators=quick_populators,
                **kw
            )

    if polymorphic_discriminator is not None:
        if polymorphic_discriminator is not mapper.polymorphic_on:
            if adapter:
                pd = adapter.columns[polymorphic_discriminator]
            else:
                pd = polymorphic_discriminator
            column_collection.append(pd)


def _instance_processor(
    mapper,
    context,
    result,
    path,
    adapter,
    only_load_props=None,
    refresh_state=None,
    polymorphic_discriminator=None,
    _polymorphic_from=None,
):
    """Produce a mapper level row processor callable
    which processes rows into mapped instances."""
    pk_cols = mapper.primary_key
    if adapter:
        pk_cols = [adapter.columns[c] for c in pk_cols]
    identity_class = mapper._identity_class
    populators = collections.defaultdict(list)
    props = mapper._prop_set
    if only_load_props is not None:
        props = props.intersection((mapper._props[k] for k in only_load_props))
    quick_populators = path.get(context.attributes, "memoized_setups", _none_set)
    for prop in props:
        if prop in quick_populators:
            col = quick_populators[prop]
            if col is _DEFER_FOR_STATE:
                populators["new"].append((prop.key, prop._deferred_column_loader))
            else:
                if col is _SET_DEFERRED_EXPIRED:
                    populators["expire"].append((prop.key, False))
                else:
                    getter = None
                    if adapter:
                        adapted_col = adapter.columns[col]
                        if adapted_col is not None:
                            getter = result._getter(adapted_col, False)
                    if not getter:
                        getter = result._getter(col, False)
                    if getter:
                        populators["quick"].append((prop.key, getter))
                    else:
                        prop.create_row_processor(
                            context, path, mapper, result, adapter, populators
                        )
        else:
            prop.create_row_processor(
                context, path, mapper, result, adapter, populators
            )

    propagate_options = context.propagate_options
    load_path = (
        context.query._current_path + path if context.query._current_path.path else path
    )
    session_identity_map = context.session.identity_map
    populate_existing = context.populate_existing or mapper.always_refresh
    load_evt = bool(mapper.class_manager.dispatch.load)
    refresh_evt = bool(mapper.class_manager.dispatch.refresh)
    persistent_evt = bool(context.session.dispatch.loaded_as_persistent)
    if persistent_evt:
        loaded_as_persistent = context.session.dispatch.loaded_as_persistent
    instance_state = attributes.instance_state
    instance_dict = attributes.instance_dict
    session_id = context.session.hash_key
    version_check = context.version_check
    runid = context.runid
    identity_token = context.identity_token
    if not refresh_state:
        if _polymorphic_from is not None:
            key = ("loader", path.path)
            if key in context.attributes:
                if context.attributes[key].strategy == (
                    ("selectinload_polymorphic", True),
                ):
                    selectin_load_via = mapper._should_selectin_load(
                        context.attributes[key].local_opts["entities"],
                        _polymorphic_from,
                    )
                selectin_load_via = mapper._should_selectin_load(
                    None, _polymorphic_from
                )
            if selectin_load_via:
                if selectin_load_via is not _polymorphic_from:
                    if not only_load_props is None:
                        raise AssertionError
                    callable_ = _load_subclass_via_in(context, path, selectin_load_via)
                    PostLoad.callable_for_path(
                        context,
                        load_path,
                        selectin_load_via.mapper,
                        selectin_load_via,
                        callable_,
                        selectin_load_via,
                    )
    post_load = PostLoad.for_context(context, load_path, only_load_props)
    if refresh_state:
        refresh_identity_key = refresh_state.key
        if refresh_identity_key is None:
            refresh_identity_key = mapper._identity_key_from_state(refresh_state)
        else:
            refresh_identity_key = None
        if mapper.allow_partial_pks:
            is_not_primary_key = _none_set.issuperset
        else:
            is_not_primary_key = _none_set.intersection

        def _instance(row):
            if refresh_identity_key:
                state = refresh_state
                instance = state.obj()
                dict_ = instance_dict(instance)
                isnew = state.runid != runid
                currentload = True
                loaded_instance = False
            else:
                identitykey = (
                    identity_class,
                    tuple([row[column] for column in pk_cols]),
                    identity_token,
                )
                instance = session_identity_map.get(identitykey)
                if instance is not None:
                    state = instance_state(instance)
                    dict_ = instance_dict(instance)
                    isnew = state.runid != runid
                    currentload = not isnew
                    loaded_instance = False
                    if version_check:
                        if not currentload:
                            _validate_version_id(mapper, state, dict_, row, adapter)
                        else:
                            if is_not_primary_key(identitykey[1]):
                                return
                            isnew = True
                            currentload = True
                            loaded_instance = True
                            instance = mapper.class_manager.new_instance()
                            dict_ = instance_dict(instance)
                            state = instance_state(instance)
                            state.key = identitykey
                            state.identity_token = identity_token
                            state.session_id = session_id
                            session_identity_map._add_unpresent(state, identitykey)
                        if currentload or populate_existing:
                            if isnew:
                                if propagate_options or not populate_existing:
                                    state.load_options = propagate_options
                                    state.load_path = load_path
                            _populate_full(
                                context,
                                row,
                                state,
                                dict_,
                                isnew,
                                load_path,
                                loaded_instance,
                                populate_existing,
                                populators,
                            )
                if isnew:
                    if loaded_instance:
                        if load_evt:
                            state.manager.dispatch.load(state, context)
                        if persistent_evt:
                            loaded_as_persistent(context.session, state.obj())
                        else:
                            if refresh_evt:
                                state.manager.dispatch.refresh(
                                    state, context, only_load_props
                                )
                        if populate_existing or state.modified:
                            if refresh_state:
                                if only_load_props:
                                    state._commit(dict_, only_load_props)
                                state._commit_all(dict_, session_identity_map)
                    if post_load:
                        post_load.add_state(state, True)
                    else:
                        unloaded = state.unloaded
                        isnew = state not in context.partials
                        if not isnew or unloaded or populators["eager"]:
                            to_load = _populate_partial(
                                context,
                                row,
                                state,
                                dict_,
                                isnew,
                                load_path,
                                unloaded,
                                populators,
                            )
                            if isnew:
                                if refresh_evt:
                                    state.manager.dispatch.refresh(
                                        state, context, to_load
                                    )
                                state._commit(dict_, to_load)
                        if post_load:
                            if context.invoke_all_eagers:
                                post_load.add_state(state, False)
                    return instance

        if mapper.polymorphic_map:
            if not _polymorphic_from:
                if not refresh_state:
                    _instance = _decorate_polymorphic_switch(
                        _instance,
                        context,
                        mapper,
                        result,
                        path,
                        polymorphic_discriminator,
                        adapter,
                    )
        return _instance


def _load_subclass_via_in(context, path, entity):
    mapper = entity.mapper
    zero_idx = len(mapper.base_mapper.primary_key) == 1
    if entity.is_aliased_class:
        q, enable_opt, disable_opt = mapper._subclass_load_via_in(entity)
    else:
        q, enable_opt, disable_opt = mapper._subclass_load_via_in_mapper

    def do_load(context, path, states, load_only, effective_entity):
        orig_query = context.query
        q._add_lazyload_options(
            (enable_opt,) + orig_query._with_options + (disable_opt,),
            path.parent,
            cache_path=path,
        )
        if orig_query._populate_existing:
            q.add_criteria(lambda q: q.populate_existing())
        (
            q(context.session).params(
                primary_keys=[
                    state.key[1][0] if zero_idx else state.key[1]
                    for state, load_attrs in states
                ]
            )
        ).all()

    return do_load


def _populate_full(
    context,
    row,
    state,
    dict_,
    isnew,
    load_path,
    loaded_instance,
    populate_existing,
    populators,
):
    if isnew:
        state.runid = context.runid
        for key, getter in populators["quick"]:
            dict_[key] = getter(row)

        if populate_existing:
            for key, set_callable in populators["expire"]:
                dict_.pop(key, None)
                if set_callable:
                    state.expired_attributes.add(key)

        else:
            for key, set_callable in populators["expire"]:
                if set_callable:
                    state.expired_attributes.add(key)

        for key, populator in populators["new"]:
            populator(state, dict_, row)

        for key, populator in populators["delayed"]:
            populator(state, dict_, row)

    else:
        if load_path != state.load_path:
            state.load_path = load_path
            for key, getter in populators["quick"]:
                if key not in dict_:
                    dict_[key] = getter(row)

            for key, populator in populators["existing"]:
                populator(state, dict_, row)

        else:
            for key, populator in populators["existing"]:
                populator(state, dict_, row)


def _populate_partial(
    context, row, state, dict_, isnew, load_path, unloaded, populators
):
    if not isnew:
        to_load = context.partials[state]
        for key, populator in populators["existing"]:
            if key in to_load:
                populator(state, dict_, row)

    else:
        to_load = unloaded
        context.partials[state] = to_load
        for key, getter in populators["quick"]:
            if key in to_load:
                dict_[key] = getter(row)

        for key, set_callable in populators["expire"]:
            if key in to_load:
                dict_.pop(key, None)
                if set_callable:
                    state.expired_attributes.add(key)

        for key, populator in populators["new"]:
            if key in to_load:
                populator(state, dict_, row)

        for key, populator in populators["delayed"]:
            if key in to_load:
                populator(state, dict_, row)

    for key, populator in populators["eager"]:
        if key not in unloaded:
            populator(state, dict_, row)

    return to_load


def _validate_version_id(mapper, state, dict_, row, adapter):
    version_id_col = mapper.version_id_col
    if version_id_col is None:
        return
    if adapter:
        version_id_col = adapter.columns[version_id_col]
    if (
        mapper._get_state_attr_by_column(state, dict_, mapper.version_id_col)
        != row[version_id_col]
    ):
        raise orm_exc.StaleDataError(
            "Instance '%s' has version id '%s' which does not match database-loaded version id '%s'."
            % (
                state_str(state),
                mapper._get_state_attr_by_column(state, dict_, mapper.version_id_col),
                row[version_id_col],
            )
        )


def _decorate_polymorphic_switch(
    instance_fn, context, mapper, result, path, polymorphic_discriminator, adapter
):
    if polymorphic_discriminator is not None:
        polymorphic_on = polymorphic_discriminator
    else:
        polymorphic_on = mapper.polymorphic_on
    if polymorphic_on is None:
        return instance_fn
    else:
        if adapter:
            polymorphic_on = adapter.columns[polymorphic_on]

        def configure_subclass_mapper(discriminator):
            try:
                sub_mapper = mapper.polymorphic_map[discriminator]
            except KeyError:
                raise AssertionError(
                    "No such polymorphic_identity %r is defined" % discriminator
                )
            else:
                if sub_mapper is mapper:
                    return
                return _instance_processor(
                    sub_mapper, context, result, path, adapter, _polymorphic_from=mapper
                )

        polymorphic_instances = util.PopulateDict(configure_subclass_mapper)

        def polymorphic_instance(row):
            discriminator = row[polymorphic_on]
            if discriminator is not None:
                _instance = polymorphic_instances[discriminator]
                if _instance:
                    return _instance(row)
                return instance_fn(row)

        return polymorphic_instance


class PostLoad(object):
    """Track loaders and states for "post load" operations.
    
    """

    __slots__ = ("loaders", "states", "load_keys")

    def __init__(self):
        self.loaders = {}
        self.states = util.OrderedDict()
        self.load_keys = None

    def add_state(self, state, overwrite):
        self.states[state] = overwrite

    def invoke(self, context, path):
        if not self.states:
            return
        path = path_registry.PathRegistry.coerce(path)
        for token, limit_to_mapper, loader, arg, kw in self.loaders.values():
            states = [
                (state, overwrite)
                for state, overwrite in self.states.items()
                if state.manager.mapper.isa(limit_to_mapper)
            ]
            loader(context, path, states, self.load_keys, *arg, **kw)

        self.states.clear()

    @classmethod
    def for_context(cls, context, path, only_load_props):
        pl = context.post_load_paths.get(path.path)
        if pl is not None:
            if only_load_props:
                pl.load_keys = only_load_props
        return pl

    @classmethod
    def path_exists(self, context, path, key):
        return (
            path.path in context.post_load_paths
            and key in context.post_load_paths[path.path].loaders
        )

    @classmethod
    def callable_for_path(
        cls, context, path, limit_to_mapper, token, loader_callable, *arg, **kw
    ):
        if path.path in context.post_load_paths:
            pl = context.post_load_paths[path.path]
        else:
            pl = context.post_load_paths[path.path] = PostLoad()
        pl.loaders[token] = (token, limit_to_mapper, loader_callable, arg, kw)


def load_scalar_attributes(mapper, state, attribute_names):
    """initiate a column-based attribute refresh operation."""
    session = state.session
    if not session:
        raise orm_exc.DetachedInstanceError(
            "Instance %s is not bound to a Session; attribute refresh operation cannot proceed"
            % state_str(state)
        )
    has_key = bool(state.key)
    result = False
    if attribute_names:
        attribute_names = attribute_names.intersection(mapper.attrs.keys())
    if mapper.inherits:
        if not mapper.concrete:
            statement = mapper._optimized_get_statement(state, attribute_names)
            if statement is not None:
                result = load_on_ident(
                    session.query(mapper)
                    .options(strategy_options.Load(mapper).undefer("*"))
                    .from_statement(statement),
                    None,
                    only_load_props=attribute_names,
                    refresh_state=state,
                )
    if result is False:
        if has_key:
            identity_key = state.key
        else:
            pk_attrs = [mapper._columntoproperty[col].key for col in mapper.primary_key]
            if state.expired_attributes.intersection(pk_attrs):
                raise sa_exc.InvalidRequestError(
                    "Instance %s cannot be refreshed - it's not  persistent and does not contain a full primary key."
                    % state_str(state)
                )
            identity_key = mapper._identity_key_from_state(state)
        if (
            _none_set.issubset(identity_key)
            and not mapper.allow_partial_pks
            or _none_set.issuperset(identity_key)
        ):
            util.warn_limited(
                "Instance %s to be refreshed doesn't contain a full primary key - can't be refreshed (and shouldn't be expired, either).",
                state_str(state),
            )
            return
        result = load_on_ident(
            session.query(mapper),
            identity_key,
            refresh_state=state,
            only_load_props=attribute_names,
        )
    if has_key:
        if result is None:
            raise orm_exc.ObjectDeletedError(state)
